/***
This do file plots %change in low-wage employment against median rent at the ZIP
level using Earnin data.
***/

*-------------------------------------------------------------------------------
**# 0. Setup
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning) == 0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results"
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml"

*-------------------------------------------------------------------------------
**# 1. Prepare data for plotting
*-------------------------------------------------------------------------------

* Load employment data
project, uses("${root}/data/web/data/Earnin - ZCTA - 2020.csv")
import delimited "${root}/data/web/data/Earnin - ZCTA - 2020.csv", clear
gisid zcta

* Re-format employment index
replace emp_incq1_apr2020 = (emp_incq1_apr2020 - 1) * 100
replace emp_incq1_jul2020 = (emp_incq1_jul2020 - 1) * 100

* Merge in median rent
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
merge 1:1 zcta using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", keep(1 3) keepusing(med_2br_2014_2018_est) nogen
rename med_2br_2014_2018_est median_2br_rent_2018

* Merge in weights
project, uses("${root}/data/derived/LODES/ZCTA mean work rent low_pay_jobs.dta")
merge 1:1 zcta using "${root}/data/derived/LODES/ZCTA mean work rent low_pay_jobs.dta", keep(1 3) nogen keepusing(low_pay_jobs)

* Merge counties for clustered standard errors
preserve
	project, uses("${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt")
	import delimited "${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt", encoding(ISO-8859-2) clear
	sort zcta5 poppt
	bysort zcta5 (poppt): keep if _n == _N                                      // only keeps the county where most of the zcta lives
	gen county_fips = 1000 * state + county
	keep zcta5 county_fips
	rename zcta5 zcta
	tempfile zcta_county_CW
	save `zcta_county_CW'
restore

merge 1:1 zcta using `zcta_county_CW', keep(1 3) nogen

*-------------------------------------------------------------------------------
**# 2. Plot binscatter
*-------------------------------------------------------------------------------

* Now get the slope and std err for July 2020 for the graph
reg emp_incq1_jul2020 median_2br_rent_2018 [aweight = low_pay_jobs], cluster(county_fips)
local beta = round(_b[median_2br_rent_2018], 0.00001) * 1000
local se = round(_se[median_2br_rent_2018], 0.00001) * 1000
local beta = string(`beta', "%3.2f")
local se = string(`se', "%3.2f")

* Make binscatter
binscatter emp_incq1_jul2020 median_2br_rent_2018 [aweight = low_pay_jobs] ///
	, ///
	${title_`version'} ///
	aspectratio(0.75) ///
	ytitle("Change in Employment (%)" "from January to July 2020") ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	text( -36.5 500 "Slope = `beta'%/$1000 (s.e. = `se')", place(ne) just(left) color(gs8) size(medsmall)) ///
	ylab( -20 "-20%" -25 "-25%" -30 "-30%" -35 "-35%", nogrid) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	legend(off) xsize(16) ysize(13)

oi_graph_export "${root}/results/Employment/Low-wage Employment vs Median Rent by Zip Binscatter", type(${fig_type})

yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml", ///
	key("emp_rent_slope_zip") ///
	comment("Slope (%/$1000)") ///
	value(`beta') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml", ///
	key("emp_rent_se_zip") ///
	comment("SE") ///
	value(`se') fmt(%9.2f)

*-------------------------------------------------------------------------------
**# 3. Additional Stats for paper
*-------------------------------------------------------------------------------

* Rent ventiles
gquantiles rent_ventile = median_2br_rent_2018 [w = low_pay_jobs] if !mi(low_pay_jobs, emp_incq1_apr2020), xtile nq(20)

* Winsorize
sum emp_incq1_apr2020 [w = low_pay_jobs], d
local p1 = r(p1)
local p99 = r(p99)
replace emp_incq1_apr2020 = `p1' if emp_incq1_apr2020 < `p1' & !mi(emp_incq1_apr2020)
replace emp_incq1_apr2020 = `p99' if emp_incq1_apr2020 > `p99' & !mi(emp_incq1_apr2020)

* Employment changes in lowest and highest rent ventiles in April 2020
sum emp_incq1_apr2020 [w = low_pay_jobs] if rent_ventile == 1
local emp_change_apr_rent_p5: di %2.0f abs(r(mean))

sum emp_incq1_apr2020 [w = low_pay_jobs] if rent_ventile == 20
local emp_change_apr_rent_p100: di %2.0f abs(r(mean))

di `emp_change_apr_rent_p5'
di `emp_change_apr_rent_p100'

* Export numbers
yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml", ///
	key("emp_change_apr_rent_p5") ///
	comment("Emp change in Apr 2020, first rent ventile") ///
	value(`emp_change_apr_rent_p5') fmt(%2.0f)
yamlout using "${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml", ///
	key("emp_change_apr_rent_p100") ///
	comment("Emp change in Apr 2020, last rent ventile") ///
	value(`emp_change_apr_rent_p100') fmt(%2.0f)

project, creates("${root}/results/paper numbers/`category'/Low-Wage Employment vs Median Rent (zip).yaml")
